<?php
require_once("../../../config.inc.php");
require_once("../../../app/_inc/include.php");
require_once "../_inc/procFunction.php";
require_once("./_inc/frameFunction.php");

$errMsg = "";
$process_unit = sxREQ('pu');
$frame = sxREQ('frame');

if(sxREQ("reqtype") == "delete"){
	$errMsg .= frameDel();
}
if(sxREQ("reqtype") == "trasfvar"){
	$errMsg .= frameTrasfVar();
}
$sSQL = "SELECT f.*, fa.alias, la.layout_alias, la.entity, " .
		"    pa.entity AS pa_entity, pa.description as pa_des, " .
		" l.description AS l_des ".
		" FROM frame f " .
		" LEFT JOIN layout l ON l.layout = f.layout " .
		" LEFT JOIN layout_alias la ON la.layout = f.layout " .
		" LEFT JOIN frame_alias_match fa ON fa.process_unit = f.process_unit AND fa.frame = f.frame " .
		"    AND fa.layout_alias = la.layout_alias " .
		" LEFT JOIN pu_alias pa ON pa.process_unit = f.process_unit AND pa.alias = fa.alias " .
		" WHERE f.process_unit='$process_unit' AND f.frame='$frame' " .
		" ORDER BY la.layout_alias";
$objSDAO->query($sSQL, "frm");
$row = $objSDAO->fetch("frm");
$layout = $row["layout"];
$description = $row["description"];
$display_only = $row["display_only"];
$l_des = $row["l_des"];
$note = $row["note"];			
//output HTML
$aOpz = array("kMenu"=>'pro.pru', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));

echo(bodyHTML($aOpz));
?>
<script type="text/javascript">
<!--
function confirmDelete() {
	if (confirm("Confirm ?")) {
		document.location.href = "<?="frameView.php?pu=$process_unit&frame=$frame&reqtype=delete&xsn=".XSN?>";
	}
}
//-->
</script>
<?
echo getPuHeader($process_unit);

$aCmd = array(
	"trasfvar"=>array("pag"=>"frameView.php?pu=$process_unit&frame=$frame&layout=$layout&reqtype=trasfvar",
						"title"=>_t("make variable from layout"),
						"text"=>_t("make variable from layout"),
						"perm"=>"viewmod",align=>'L'),
	"alias"=>array("pag"=>"frameAliasList.php?pu=$process_unit&frame=$frame",
						"title"=>_t("linked alias"),
						"text"=>_t("linked alias"),
						"perm"=>"viewmod"),
	"mod"=>array("pag"=>"frameMod.php?pu=$process_unit&frame=$frame",
						"title"=>_t("changee"),
						"text"=>_t("change"),
						"perm"=>"viewmod"),
	"del"=>array("pag"=>"javascript:confirmDelete();",
						"title"=>_t("delete"),
						"text"=>_t("delete"),
						"perm"=>"viewmod"),
	"back"=>array()
	);
$sCmd = (commandsHTML($aCmd));	
echo $sCmd;	

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='2'>Frame</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Name</td>" .
			"<td class=\"text01\">$frame</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Description</td>" .
			"<td class=\"text01\">".sxHE($description)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Layout</td>" .
			"<td class=\"text01\">" .
			"<a class=\"text01\" href=\"../layo/layoutView.php?layout=".$layout."&xsn=".XSN."\">".
			"$layout</a> - $l_des</td>" .
		"</tr>\n");
$chk = "";
if($display_only == "Y")
	$display_only = "yes";	
else
	$display_only = "no";	
echo("<tr>" .
			"<td class=\"labl01\">Display only</td>" .
			"<td class=\"text01\">$display_only</td>" .
		"</tr>\n");
		
echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\"><pre class=\"text01\">".sxWW(sxHE($note))."</pre></td>" .
		"</tr>\n");
echo("</table>");

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='2'>Aliases</td>" .
		"</tr>\n");
	echo("<tr>" .
				"<td class=\"tabh01\" width='30%'>Layout alias</td>" .
				"<td class=\"tabh01\" width='70%'>Process-unit alias</td>" .
			"</tr>\n");
while($row) {
	echo("<tr>" .
				"<td class=\"text01\">".$row['layout_alias'].((empty($row['entity']))?'':" - ".$row['entity'])."</td>");
	if (ereg("[WX]",$row['alias'])) {
		echo ("<td class=\"text01\">".$row['alias']." - " .$row['pa_des']);
	} elseif (empty($row['pa_entity'])) {
		echo ("<td class=\"text01\" style='color:red;font-weight:bold;'>"." -- not matched --");
	} else {
		echo ("<td class=\"text01\">".$row['alias']." - " .$row['pa_entity']);
		if (!empty($row['pa_des'])) echo (" - " .$row['pa_des']);
	}
	echo ("</td>" .
			"</tr>\n");
	$row = $objSDAO->fetch("frm");		
}
echo("</table>");

echo $sCmd;

echo(footerHTML());
exit();

function frameTrasfVar() {
	global $objSDAO;
	$sSQL = "SELECT * FROM layout_variable WHERE layout = '".sxRAS('layout')."'";
	$objSDAO->query($sSQL,'ftv');
	while($row=$objSDAO->fetch('ftv')) {
		$sSQL = "INSERT INTO pu_variable (process_unit, alias, variable, description, note, read_only) " .
				" values('".sxRAS('pu')."'," .
				" '".sxAS($row['layout_alias'])."', " .
				" '".sxAS($row['variable'])."', " .
				" '".sxAS($row['variable'])."', " .
				" '', " .
				" 'N')";
		$objSDAO->exec($sSQL,array('contOnErr'=>true));
	}
	return "Variables transferred<br>";
}

function frameDel(){
 	global $objSDAO;
	$errMsg = "";
	$sSQL = "DELETE FROM frame " .
		"WHERE process_unit='".sxRAS('pu')."' AND frame='".sxRAS('frame')."'";	
	$objSDAO->exec($sSQL);
	
	$sSQL = "DELETE FROM frame_alias_match " .
		"WHERE process_unit = '".sxRAS('pu')."' AND frame = '".sxRAS('frame')."'";	
	$objSDAO->exec($sSQL);
	
	$_SESSION[USEROBJ]->reLoad();
	$url = $_SESSION[USEROBJ]->getHistory(-1, true);
	serverRedir($url);
	return 'abnormal end';
}

